import logging
import os

logging.getLogger("itchat").setLevel(logging.ERROR)


class LogFactory:

    def __init__(self, name):
        self.name = name
        self.formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    def get_logger(self):
        logger = logging.getLogger(self.name)
        logger.setLevel(logging.DEBUG)
        if not logger.hasHandlers():
            self._set_handlers(logger)
        return logger

    def _set_handlers(self, logger):
        ch = logging.StreamHandler()
        ch.setFormatter(self.formatter)
        level = int(os.environ.get('LOGLEVEL', logging.INFO))
        ch.setLevel(level)
        logger.addHandler(ch)
